﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AddonService.DTO;

namespace AddonService.Data
{
    public class DataProvider
    {
        public static void InsertAnnotation(Annotation annotation)
        {
            try
            {
                AddonDataContext db = new AddonDataContext();
                tbl_Annotation addon = new tbl_Annotation();
                addon.Page_Url = annotation.Url;
                addon.Tag_Id = annotation.TagId;
                addon.Page_Content = annotation.Content;
                addon.Comments = annotation.Comment;
                db.tbl_Annotations.InsertOnSubmit(addon);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Annotation GetAnnotation(int id)
        {
            AddonDataContext db = new AddonDataContext();
            var qry = from annotate in db.tbl_Annotations
                      where annotate.Annot_Id == id
                      select new Annotation
                      {
                          Id = annotate.Annot_Id,
                          Url = annotate.Page_Url,
                          TagId = annotate.Tag_Id,
                          Comment = annotate.Comments,
                          Content = annotate.Page_Content
                      };
            return qry.ToList<Annotation>().First();
        }

        public static List<Annotation> GetAllAnnotation()
        {
            AddonDataContext db = new AddonDataContext();
            var qry = from annotate in db.tbl_Annotations
                      select new Annotation
                      {
                          Id = annotate.Annot_Id,
                          Url = annotate.Page_Url,
                          TagId = annotate.Tag_Id,
                          Comment = annotate.Comments,
                          Content = annotate.Page_Content
                      };
            return qry.ToList<Annotation>();
        }

        public static void DeleteAnnotation(string url)
        {
            //int k = Convert.ToInt32(id);
            AddonDataContext db = new AddonDataContext();

            var qry1 = from annotate in db.tbl_Annotations
                       where annotate.Page_Url == url
                       select annotate;
            List<tbl_Annotation> annot = qry1.ToList();

            db.tbl_Annotations.DeleteAllOnSubmit(annot);
            db.SubmitChanges();
        }

        /*public static void DeleteAnnotation(String Url)
        {
            AddonDataContext db = new AddonDataContext();
            var qry = (from annotate in db.tbl_Annotations
                       where annotate.Page_Url == Url
                       select annotate).First();
            db.tbl_Annotations.DeleteOnSubmit(qry);
            db.SubmitChanges();
        }*/
    }
}